package net.whg.stream;

import java.math.BigDecimal;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.*;
import java.util.concurrent.atomic.AtomicReference;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class FilterFindFirstStream {

    public static void main(String[] args) {

        System.out.println(generateRandomString(16));

        Pattern pattern = Pattern.compile(".*[\\;\\/].*");
        String remark = "0.000.00";
        if (pattern.matcher(remark).matches()){
            System.out.println("match");
        }else {
            System.out.println("not match");
        }

        System.out.println(Byte.parseByte("0"));

        System.out.println(handleMedicineName("*医疗服务*大生化（TB+DB+TP+ALB+ALT+AST+GGT+ALP+GLDH+LAP+PA+TRF+CHE+TBA+UREA+CR+UA+eGFR+GLU+TG+CH+HDL+LDL+AI+LDH+ADA+K+NA+CL)"));

        List<String> dateList = new ArrayList<>();
        dateList.add("2020-01-01");
        dateList.add("2020-01-02");
        dateList.add("2024-01-01");
        String minDate = dateList.stream().min(String::compareTo).get();
        System.out.println(minDate);

        formatAmount("123.1.1");
        List<Student1> numList = new ArrayList<>();
        numList.add(new Student1("张三", new BigDecimal(18)));
        numList.add(new Student1("张三1", new BigDecimal(18)));
        numList.add(new Student1("张三2", new BigDecimal(18)));
        BigDecimal reduce = numList.stream().map(Student1::getHeight)
                .filter(height -> height.compareTo(new BigDecimal(40)) > 0).reduce(BigDecimal.ZERO,  BigDecimal::add);
        System.out.println("reduce:" + reduce);

        List<Student> list1 = new ArrayList<>();
        list1.add(null);
        list1.add(new Student("张三", 18));
        test(list1);
        System.out.println(list1);

        byte a = (byte)2;
        System.out.println(String.valueOf(a));

        String b = "3";
        byte bByte = Byte.parseByte(b);
        System.out.println(bByte);

        double[] numbers = new double[] {925.64 ,113.18 ,123.06 ,171 ,149.54 ,300.08 ,669.73 ,1414.07 ,169.42 ,60 ,472.62 ,598.22 ,84.64 ,420 ,632.08 ,595.16 ,467.83 ,368.26 ,892.45 ,382.6 ,169 ,473.77 ,470.62 ,2516.63 ,15.09 ,120 ,284.34 ,352.8 ,80.92 ,1203.49 ,176 ,1273.88 ,1282.28 ,688.68 ,585.51 ,278.61 ,238 ,1262.87 ,258.58 ,1870.9 ,622 ,505.92 ,431.58 ,174 ,417.96 ,1066.16 ,934.32 ,73.38 ,20.1 ,55.26 ,435.2 ,659.31 ,621.08 ,610.83 ,610.49 ,659.31 ,695.44 ,160 ,994.72 ,840.31 ,179.95 ,216.15 ,190.89 ,244.16 ,940.9 ,293.51 ,1838.82 ,1620.62 ,141.14 ,130.4 ,714.02 ,176.4 ,24.96 ,220.82 ,1792.52 ,367.02 ,908.2 ,1543.8 ,284.41 ,1764.98 ,433.3 ,805.81 ,1566.2 ,286.23 ,924 ,183.04 ,665.08 ,1915.38 ,170.45 ,153.47 ,754.13 ,518.11 ,195.26 ,272.59 ,268.86 ,241.53 ,600.88 ,375.26 ,585.78 ,99.09 ,952.29 ,560.67 ,39.26 ,85.64 ,231 ,684.16 ,2070.28 ,6 ,153.86 ,791.1 ,272.81 ,417.64 ,443.82 ,802.45 ,499.93 ,1117.93 ,816.93 ,463.95 ,1152.61 ,391.91 ,622.45 ,712.62 ,57.27 ,114 ,57.27 ,248.87 ,231.12 ,433.27 ,762.5 ,37.45 ,1548.54 ,986.5 ,911.31 ,286.96 ,198.4 ,293.86 ,363.22 ,337.76 ,1021.88 ,867.24 ,358.07 ,355.92 ,1126.36 ,120.29 ,443.17 ,461 ,413.53 ,457.49 ,524.44 ,637.4 ,1343.43 ,760.76 ,1491.54 ,866.58 ,2546.76 ,29.59 ,336.44 ,775.4 ,200.19 ,231.2 ,38.7 ,64.62 ,273.41 ,751.7 ,633.68 ,208 ,56.34 ,117.49 ,1150.57 ,280.78 ,590.89 ,89.16 ,507.34 ,592.3 ,793.73 ,183.2 ,549 ,567.37 ,308.6 ,659.72 ,353.93 ,392.71 ,98.08 ,915.16 ,482.28 ,402.37 ,530.39 ,414.23 ,205 ,356.06 ,1147.36 ,1345.68 ,671.63 ,241.42 ,383.63 ,908.7 ,801.74 ,3090.56 ,733.75 ,1277.13 ,330.95 ,326.6 ,335.8 ,433.7 ,292.34 ,2124.23 ,757.13 ,2264.79 ,658.61 ,1058.97 ,2126.68 ,584.82 ,974.11 ,512.68 ,371.62 ,653.47 ,141.81 ,423.45 ,716.28 ,6497.53 ,766.08 ,141.64 ,827.34 ,698.11 ,351.15 ,790.9 ,589.28 ,206.03 ,206.03 ,379.06 ,20.58 ,16 ,294.07 ,311.93 ,838.04 ,21.24 ,316.1 ,790.73 ,592.8 ,230.86 ,551.06 ,249 ,676.09 ,11912.11 ,910.56 ,355.76 ,614.68 ,18.76 ,421.53 ,949 ,571.08 ,2958.64 ,165.75 ,250.4 ,161.81 ,530.39 ,485.74 ,77.7 ,1071.56 ,446.2 ,1097 ,544.43 ,1125.53 ,565.39 ,100.09 ,155.7 ,540.25 ,974 ,714 ,207.16 ,1167.9 ,512.6 ,921.62 ,1062.21 ,610.56 ,1128.43 ,221.84 ,921.89 ,308.46 ,694.15 ,470 ,1943.8 ,545.34 ,617 ,384.16 ,1440 ,1440 ,800 ,610.33 ,679.76 ,505.94 ,1108.41 ,1011.38 ,557.83 ,2814.13 ,356.6 ,1747.94 ,2413.52 ,825.86 ,876.96 ,397.1 ,825.86 ,966.56 ,1118.11 ,1090.18 ,1440.92 ,420.71 ,1209.32 ,771.41 ,880.67 ,280.52 ,149.56 ,657.04 ,356.02 ,954.38 ,821.64 ,142.87 ,628.8 ,365.08 ,551.4 ,278 ,1099.24 ,670.76 ,963.66 ,878.88 ,1968.76 ,140.6 ,640.71 ,684.55 ,1046.16 ,55.7 ,1456.8 ,612.32 ,151.91 ,55.7 ,241.22 ,89.31 ,702 ,1148 ,763.77 ,502.98 ,848 ,396.48 ,1619.43 ,784.7 ,604.91 ,563.67 ,346.04 ,828.34 ,731.4 ,1008.91 ,422.62 ,310.62 ,901.76 ,866.43 ,97.26 ,1053.51 ,410.59 ,4095 ,330.8 ,339 ,477 ,157.52 ,607.26 ,912.03 ,497.08 ,483.26 ,1360.6 ,1478.33 ,1381.9 ,504 ,1005.3 ,1329.1 ,150.12 ,127 ,918.8 ,592 ,590.45 ,1130.53 ,830.93 ,489.24 ,316.97 ,835.92 ,72 ,324.46 ,695.57 ,719.1 ,324.46 ,319.92 ,41.18 ,580 ,927.56 ,588.2 ,246.63 ,555.82 ,1155 ,114 ,32.27 ,749.32 ,236.18 ,252.42 ,1075.19 ,620.74 ,630.98 ,736.27 ,118.3 ,260.4 ,1178.18 ,2801.8 ,754.74 ,551.38 ,554.29 ,953.71 ,537.66 ,946.99 ,1249.57 ,1154.27 ,24.88 ,244.3 ,1077.02 ,562.69 ,771.97 ,1285.48 ,247.25 ,1439.25 ,159.87 ,392.76 ,144 ,664.29 ,137.7 ,1563.08 ,342.1 ,645.4 ,491 ,865.94 ,395.94 ,671.34 ,598.43 ,385.09 ,293.8 ,461.84 ,207.15 ,2243.04 ,1166.14 ,120.96 ,590.86 ,1487.56 ,782.41 ,782.41 ,1126.46 ,172.38 ,709.72 ,781.71 ,728.32 ,405.95 ,243.21 ,428.31 ,276.34 ,676.41 ,875.24 ,417.9 ,1064.16 ,258.56 ,1288.59 ,453.49 ,299.17 ,307.17 ,509.69 ,531.29 ,2419.02 ,262 ,634.17 ,2069.2 ,762 ,297.3 ,806.31 ,1042.02 ,1201.86 ,1465.86 ,271.49 ,999.42 ,684.11 ,869.83 ,96.21 ,980.81 ,774.56 ,1693.46 ,902.03 ,1843.46 ,611.81 ,628.21 ,174.25 ,285.46 ,696 ,1419.4 ,1099.47 ,359 ,696.08 ,334.91 ,632.74 ,142.93 ,370.39 ,231.48 ,108 ,122.74 ,178.68 ,76.24 ,76.24 ,371.31 ,152.5 ,530 ,1299.18 ,925.28 ,572.04 ,185.14 ,652.88 ,188.54 ,1153.5 ,1153.5 ,2321.54 ,1153.5 ,834.87 ,1248.34 ,990.6 ,788.55 ,561.32 ,645.79 ,789.91 ,108.24 ,108.24 ,1793.53 ,144.94 ,1793.53 ,1793.53 ,1793.53 ,357 ,116.09 ,311.88 ,55.58 ,115.77 ,433.98 ,302.91 ,454 ,150 ,75.66 ,265.3 ,787.14 ,36.67 ,325.95 ,663.41 ,658.36 ,883.13 ,375.76 ,73.07 ,375.76 ,103.68 ,206.95 ,1725.18 ,46.03 ,1007.64 ,1093 ,248 ,1146.03 ,1172.2 ,638.68 ,613.8 ,638.68 ,445.1 ,654.05 ,515.13 ,127.99 ,888.28 ,605.34 ,159 ,1161.29 ,254.15 ,848.26 ,1213.96 ,1960.92 ,2688 ,1340 ,2448 ,531.42 ,618 ,217.2 ,1428.55 ,1629.1 ,618.8 ,467.26 ,112.9 ,817.4 ,173.93 ,124.81 ,105.33 ,527.45 ,1038.71 ,1121.28 ,1176.26 ,785.14 ,784.34 ,71.86 ,71.86 ,565.3 ,239.16 ,85.33 ,597.29 ,1251.59 ,1063.57 ,843.51 ,653.75 ,247.6 ,597.9 ,1764.5 ,54.1 ,56.4 ,197.81 ,316.19 ,86.59 ,138.21 ,677.09 ,2858.11 ,749.15 ,160.78 ,353.94 ,504.65 ,942.37 ,226.27 ,755.67 ,849.7 ,731.49 ,626.04 ,317.7 ,740 ,105.4 ,113.15 ,152 ,840 ,118.92 ,498.25 ,1501.25 ,249.74 ,289.35 ,779.33 ,596 ,1467.1 ,858.54 ,509 ,836.1 ,852.45 ,803.17 ,968.75 ,211.61 ,650.48 ,570 ,32.78 ,399.6 ,541.22 ,215.73 ,367.93 ,180.25 ,677.01 ,1246.28 ,273.33 ,235.32 ,136 ,810.67 ,260.73 ,211.94 ,450.59 ,619.21 ,20.96 ,887.17 ,626.6 ,372.16 ,248.4 ,383.95 ,44.85 ,319.7 ,357 ,43.42 ,404.58 ,123 ,123 ,123 ,123 ,484.7 ,1432.57 ,1235.87 ,211.6 ,175.19 ,308.9 ,379.83 ,528 ,594.64 ,682.48 ,1343.7 ,1361.35 ,1043.3 ,1296.02 ,1402.7 ,1240.63 ,1301.45 ,545.68 ,256.63 ,232.61 ,629.2 ,232.61 ,127.34 ,301.49 ,371.25 ,388.58 ,974 ,224.77 ,598.15 ,404.25 ,94.6 ,470.88 ,510.56 ,67.13 ,604.55 ,777 ,1224.7 ,194.05 ,130.15 ,570.68 ,143.05 ,239.9 ,182.62 ,317.12 ,782.4 ,503.76 ,613.76 ,628.91 ,1040.79 ,1177.06 ,843.95 ,563.41 ,1028.67 ,697.92 ,615.6 ,146 ,528.68 ,240.11 ,692.14 ,226.92 ,725.71 ,827.95 ,128.74 ,275.53 ,321.98 ,321.98 ,369.71 ,193.61 ,321.98 ,273.73 ,2317.41 ,163.61 ,434.83 ,223.49 ,237.23 ,351.74 ,231.6 ,439.23 ,401.6 ,1147.4 ,375.55 ,703.94 ,184.99 ,59.97 ,414.11 ,488.48 ,1358 ,213.49 ,669.56 ,483.56 ,322.78 ,341.39 ,768.35 ,1238.68 ,796.08 ,359.03 ,425.69 ,371.6 ,1352.28 ,943.11 ,877.39 ,295.56 ,418.49 ,1216.52 ,760.3 ,1699.25 ,118.46 ,277.91 ,167.75 ,545.38 ,823.68 ,462.43 ,99 ,310.37 ,534.88 ,1454.51 ,532 ,1127 ,119.97 ,683.27 ,275.77 ,267.77 ,294.7 ,591.6 ,183.7 ,755.51 ,133.18 ,620.33 ,147.94 ,202.87 ,552.6 ,342.3 ,1021.84 ,304.11 ,1690.04 ,339.89 ,240.63 ,69.86 ,1170.06 ,253.99 ,268.56 ,287.21 ,268.69 ,522.25 ,550.63 ,183.45 ,210.71 ,272.52 ,177.53 ,266.74 ,432 ,551.76 ,600.89 ,1005.87 ,722.86 ,817.07 ,856.73 ,217.15 ,1038.35 ,582.18 ,289.6 ,463.88 ,171.14 ,56.96 ,89.59 ,325.99 ,2427.37 ,259.26 ,695.3 ,528.36 ,593.26 ,1460.62 ,432.73 ,543.4 ,116.24 ,884.27 ,421.58 ,167.42 ,515.28 ,446.2 ,1651.13 ,1378.13 ,1265.42 ,1578.07 ,1587.82 ,696.26 ,317.51 ,317.51 ,1851.53 ,722.97 ,937.9 ,833.5 ,286.55 ,525.64 ,626 ,348 ,518.18 ,1439.17 ,1674.49 ,268.04 ,447 ,131.2 ,959.59 ,172 ,483.12 ,906.48 ,234.22 ,99.89 ,590.45 ,528.75 ,508.76 ,56.4 ,944 ,180.66 ,1752.96 ,1778.93 ,241.41 ,43.57 ,806 ,2471.41 ,802.6 ,625.82 ,16.45 ,106.56 ,137.29 ,20 ,257.28 ,196.2 ,1055.56 ,479.1 ,102.9 ,1467.34 ,1499.08 ,1320.82 ,558.87 ,1314.72 ,1291.7 ,433.8 ,1272.85 ,603.96 ,404.25 ,607.9 ,836.27 ,313.64 ,1547.18 ,779.13 ,1461.6 ,132.46 ,171.6 ,120.6 ,589.58 ,309.13 ,907.73 ,118.95 ,907.73 ,232.65 ,1175.32 ,532 ,1254.79 ,1665.5 ,1587.85 ,158.1 ,141.21 ,115.74 ,123.75 ,115.74 ,420.26 ,469.46 ,1478.77 ,1037.97 ,920.56 ,886 ,742 ,828.77 ,637 ,661.13 ,423 ,1218.08 ,1058.27 ,383.42 ,104.32 ,114 ,537.15 ,114 ,779.69 ,317.02 ,317.28 ,276.29 ,177.35 ,355.15 ,31.64 ,203.83 ,275.94 ,248.56 ,297.35 ,160.2 ,203.6 ,208.2 ,11.6 ,820.27 ,733.62 ,584.48 ,468.74 ,154.38 ,260 ,261.5 ,255.5 ,308 ,1659.84 ,114 ,2411.4 ,747.44 ,1825.28 ,600 ,433.4 ,413.59 ,104.66 ,635.36 ,170.63 ,199.36 ,1861.3 ,571.64 ,653.7 ,1328.64 ,1348.27 ,995.24 ,555.01 ,698.28 ,108.52 ,262.34 ,2172.14 ,126.95 ,792.38 ,716.28 ,91.49 ,197.94 ,124.25 ,54.76 ,114 ,1153.85 ,479 ,855.24 ,964.34 ,526 ,830.44 ,479.72 ,127.7 ,84.94 ,84.94 ,80.44 ,80.44 ,879.25 ,1578.39 ,1043 ,598.6 ,293.2 ,808.5 ,78.96 ,1189.87 ,601 ,236 ,472.99 ,1171.56 ,1347.77 ,1320.49 ,1518.78 ,893.94 ,1194.28 ,504 ,1595.05 ,218.46 ,807.62 ,611 ,335.24 ,623.29 ,229.23 ,2758.8 ,292.44 ,434.96 ,556.93 ,810.44 ,90 ,586.49 ,846.96 ,463.75 ,519.7 ,908.93 ,741.91 ,902.63 ,874.23 ,501.6 ,416 ,178.96 ,252 ,801.87 ,823.33 ,725.05 ,692.34 ,1008.41 ,667 ,732.06 ,680.93 ,727.27 ,872.37 ,273.68 ,2066.07 ,398.12 ,144.08 ,44.09 ,158 ,416.34 ,230.71 ,33.22 ,662 ,1257.48 ,771 ,1106.03 ,1360.6 ,419.2 ,1557.04 ,141.32 ,411.2 ,1605.34 ,235 ,686.26 ,691.36 ,565.97 ,482.91 ,676.86 ,144.33 ,1965.27 ,493.51 ,552.03 ,584.79 ,1989.05 ,390.43 ,644.72 ,248.75 ,2349.84 ,981.7 ,250 ,815.84 ,860.59 ,402.09 ,2369.84 ,652 ,704.62 ,417 ,286.5 ,573 ,286.5 ,323.68 ,1246.81 ,209.75 ,638.89 ,112.15 ,275.96 ,1572.75 ,40.45 ,500.18 ,254.98 ,263.29 ,747.14 ,262.72 ,298.96 ,527.47 ,811.57 ,927.94 ,1024.92 ,802.67 ,216.95 ,63.32 ,475.76 ,166.4 ,568.5 ,87.2 ,680.14 ,595.7 ,259.1 ,351.55 ,902.91 ,356 ,189.85 ,382.98 ,393.92 ,716.13 ,181.2 ,1666.04 ,398.77 ,150 ,1961.59 ,596.55 ,502 ,273.49 ,685 ,479.98 ,1376 ,730.5 ,888.99 ,394.5 ,643.96 ,748.98  };
        System.out.println("数量" + Arrays.stream(numbers).distinct().count());

        Student[] students = new Student[] {
                new Student("张三", 18),
                new Student("李四", 19),
                new Student("王五", 20),
                new Student("赵六", 21),
                new Student("钱七", 22),
                new Student("孙八", 23),
                new Student("周九", 24),
                new Student("吴十", 25),
                new Student("郑十一", 26),
                new Student("王十二", 27),
                new Student("张三", 25)
        };

        AtomicReference<String> name = new AtomicReference<>();
        Stream.of(students)
                .filter(student -> student.getAge() == 50)
                .findFirst().ifPresent(student -> name.set(student.getName()));
        System.out.println(name.get());

        List<Student> list = Arrays.asList(students);
        Map<String,Integer> map = list.stream().collect(Collectors.toMap(Student::getName, Student::getAge,(v1, v2)->v1));
        System.out.println(map);

    }

    public static String generateRandomString(int length) {
        String uuid = UUID.randomUUID().toString().replaceAll("-", "");
        if (uuid.length() < length) {
            throw new IllegalArgumentException("无法生成超过 " + uuid.length() + " 位的随机字符串");
        }
        return uuid.substring(0, length);
    }

    private static class Student {
        private String name;
        private int age;

        public Student(String name, int age) {
            this.name = name;
            this.age = age;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public int getAge() {
            return age;
        }

        public void setAge(int age) {
            this.age = age;
        }
    }

    private static class Student1 {
        private String name;
        private BigDecimal height;

        public Student1(String name, BigDecimal height) {
            this.name = name;
            this.height = height;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public BigDecimal getHeight() {
            return height;
        }

        public void setHeight(BigDecimal height) {
            this.height = height;
        }
    }

    public static void test(List<Student> list1){
        //遍历list1并把其中为null的元素过滤掉，不新建对象
        /*Iterator<Student> iterator = list1.iterator();
        while (iterator.hasNext()) {
            if (iterator.next() == null) {
                iterator.remove();
            }
        }*/
        list1.removeIf(Objects::isNull);
        //list1 = list1.stream().filter(Objects::nonNull).collect(Collectors.toList());
    }

    public static BigDecimal formatAmount(String amount){
        // 去掉特殊字符
        amount = amount.replaceAll(",","");
        try {
            return new BigDecimal(amount);
        } catch (Exception e) {
            System.out.println("金额格式转换异常：" +  amount);
            return null;
        }
    }

    public static String handleMedicineName(String medicineName){
        Pattern pattern = Pattern.compile("\\\\*(.*)\\\\*");
        if (pattern.matcher(medicineName).matches()) {
            medicineName = medicineName.replaceAll("\\*(.*)\\*" , "");
        }
        // 去除空格
        medicineName = medicineName.replaceAll(" ","");
        return medicineName;
    }

    public static synchronized String md5_32(String plainText) {
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            md.update(plainText.getBytes(StandardCharsets.UTF_8));
            byte[] b = md.digest();
            StringBuffer buf = new StringBuffer();

            for(int offset = 0; offset < b.length; ++offset) {
                int i = b[offset];
                if (i < 0) {
                    i += 256;
                }

                if (i < 16) {
                    buf.append("0");
                }

                buf.append(Integer.toHexString(i));
            }

            return buf.toString();
        } catch (NoSuchAlgorithmException var6) {
            NoSuchAlgorithmException e = var6;
            e.printStackTrace();
            return null;
        }
    }
}
